//
//  RTJAutomaticBidController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/12.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJAutomaticBidController.h"
#import "RTJAutoMarkAlertView.h"
#import "LewPopupViewAnimationFade.h"
#import "RTJAutomaticBidDetailsController.h"
#import "RTJAutoMarkTimeAlertView.h"

@interface RTJAutomaticBidController ()
@property (weak, nonatomic) IBOutlet UIView *bg_view;
@property (weak, nonatomic) IBOutlet UILabel *lab1;
@property (weak, nonatomic) IBOutlet UILabel *lab2;
@property (weak, nonatomic) IBOutlet UITextField *moneyFld;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (nonatomic,assign) BOOL isSelected;

@property (weak, nonatomic) IBOutlet UIButton *kaiqi;
@property (nonatomic,strong) NSString *min_rate;
@property (nonatomic,strong) NSString *max_rate;
@property (nonatomic,strong) NSString *period;
@property (nonatomic,strong) NSMutableArray *periodArr;
@property (weak, nonatomic) IBOutlet UILabel *lab3;
@property (weak, nonatomic) IBOutlet UILabel *lab4;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;

@end

@implementation RTJAutomaticBidController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.viewTop.constant = screenH == 812 ? 88 : 64;

    self.title = @"智能投标";
//    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"说明" style:UIBarButtonItemStylePlain target:self action:@selector(shuoming)];
    
    NSString *str = @"1、标的开始十分钟后，才会启动智能投标。\n2、投标序列按照开启智能投标的时间先后进行排序。\n3、每个用户每个标仅智能投标一次,可投资金投完之后,排到队尾。\n4、轮到用户投标时没有符合用户条件的标，保留排名。";
    NSString *str1 = @"智能投标是一个根据您的设置条件自动帮您完成受托的工具，您只需要设置借款期限、利率范围等条件，开启后系统即可全天候帮您进行智能投标，为您省时省力。";

    _lab3.attributedText = [self getAttributedStringWithString:str lineSpace:0];
    _lab4.attributedText = [self getAttributedStringWithString:str1 lineSpace:0];

    
    _bg_view.layer.cornerRadius = 5;
    _bg_view.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    _bg_view.layer.shadowOffset = CGSizeMake(-1, 1);
    _bg_view.layer.shadowOpacity = 0.5;
    _bg_view.layer.shadowRadius = 5;
    _periodArr = [NSMutableArray array];
   
    _btn1.selected = YES;
    _isSelected = YES;
    
//    _kaiqi.userInteractionEnabled = NO;
//    [_kaiqi setTitle:@"即将上线" forState:UIControlStateNormal];
    [_kaiqi setTitle:[RTJUserTool getTheUserModel].user_autobid == 1 ?@"修改":@"开启" forState:UIControlStateNormal];
    [self loadData];
    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];

    
    [RTJNetWork asyncRequestWithURL:@"user/auto_bid" params:nil method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [self.view removeEmptyView];

        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSDictionary *dic = data[@"data"];
                
                if ([ dic isEqual:[NSNull null] ]) {
                    
                    self.lab1.text = @"请选择";
                    self.lab2.text = @"请选择";
                    
                }else{
                    
                    NSString *period = [NSString stringWithFormat:@"%@",data[@"data"][@"period"]];
                    
                    if ([period isEqualToString:@"<null>"]) {
                        
                        self.lab1.text = @"";
                        self.period  = @"";
                    }else{
                        
                        self.lab1.text =[NSString stringWithFormat:@"%@ 个月",data[@"data"][@"period"]];;
                        self.period  = [NSString stringWithFormat:@"%@",data[@"data"][@"period"]];
                    }
                    
                    NSString *str = @"%";
                    self.lab2.text = [NSString stringWithFormat:@"%@%@ ～ %@%@",data[@"data"][@"min_rate"],str,data[@"data"][@"max_rate"],str];
                    self.moneyFld.text = [NSString stringWithFormat:@"%@",data[@"data"][@"retain_amount"]];
                    
                    self.min_rate = [NSString stringWithFormat:@"%@",data[@"data"][@"min_rate"]];
                    self.max_rate = [NSString stringWithFormat:@"%@",data[@"data"][@"max_rate"]];
                    
                }
                
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }
        
        
    }];
}


- (IBAction)btnClickAction:(UIButton *)sender {
    
    
    if (sender.tag == 10) {
        
        RTJAutoMarkTimeAlertView *view = [RTJAutoMarkTimeAlertView defaultPopupViewWithFrame:CGRectMake(0, 0,350, 265) isPayWay:NO array:@[]];
        view.parentVC = self;
        
        [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
            
            NSLog(@"动画结束");
            
        }];
        
        [view withMonthSureClick:^(NSMutableArray * _Nonnull monthArr) {
            
            [self.periodArr removeAllObjects];
            self.periodArr = monthArr;
            
           NSString *str = [monthArr componentsJoinedByString:@","];
            self.lab1.text = [NSString stringWithFormat:@"%@ 个月",str];

        }];
        
        
    }else{
        
        RTJAutoMarkAlertView *view = [RTJAutoMarkAlertView defaultPopupViewWithFrame:CGRectMake(0, 0,325, 239) type:sender.tag==10 ? YES:NO];
        view.parentVC = self;
        [self lew_presentPopupView:view animation:[LewPopupViewAnimationFade new] dismissed:^{
            
            NSLog(@"动画结束");
            
        }];
        
        [view withSureClick:^(NSString * _Nonnull minStr, NSString * _Nonnull maxStr) {
            
            
                self.min_rate = minStr;
                self.max_rate = maxStr;
                NSString *str = @"%";
                self.lab2.text = [NSString stringWithFormat:@"%@ ~ %@ %@",minStr,maxStr,str];
                
            
        }];
        
    }
    
}

- (IBAction)isSelected:(UIButton *)sender {

    sender.selected =!sender.selected;
    _isSelected = sender.selected;
}

- (IBAction)kaiqi:(id)sender {
    
    if (_lab1.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入借款期限"];
        return;
    }
    
    if (_lab2.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入利率范围"];
        return;
    }
    
    if (_moneyFld.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入账户保留资金"];
        return;
    }
    //
    
    if (_isSelected == NO) {
        
        [SVProgressHUD showErrorWithStatus:@"请认真阅读《自动受托授权书》"];
        return;
    }
    
    NSString *str =_periodArr.count ? [_periodArr componentsJoinedByString:@","] : _period;
    NSDictionary *dic = @{@"min_rate":_min_rate,
                          @"max_rate":_max_rate,
                          @"period":str,
                          @"retain_amount":_moneyFld.text,
                          @"is_effect":@"1"
                          };
    [SVProgressHUD showWithStatus:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/auto_bid_save" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                RTJUserModel *model = [RTJUserTool getTheUserModel];
                model.user_autobid = 1;
                [RTJUserTool saveUserInfo:model];
                
                [SVProgressHUD showSuccessWithStatus:@"智能投标功能已开启"];
                RTJAutomaticBidDetailsController *vc = [[RTJAutomaticBidDetailsController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
        
    }];
    
}

-(void)shuoming{
    
    
}

- (IBAction)fuwu:(id)sender {
    
    RTJHTMLViewController *vc = [[RTJHTMLViewController alloc]init];
    vc.title = @"智能投标服务授权书";
    vc.htmlStr = @"auto_tender_agreement";
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributedString;
}

@end
